# Params
# - title: A dictionary with "text" and optional "link_attrs" (a dictionary of link attributes for the title).
# - padding: Type of padding to apply to the pattern - "deep", "shallow", "default" (default is "default").
# - is_split_on_medium: Boolean to indicate if the section should be split on medium screens (default is false).
# - top_rule_variant: Type of HR to render at the top of the pattern. "default" | "muted" (default is "default").
{% from "_macros/vf_basic-section.jinja" import basic_section_item, basic_section_title %}
{% from "_macros/shared/vf_section_top_rule.jinja" import vf_section_top_rule %}

{% macro _get_item_padding_classes(item_config={}, isLastLoopItr=False, isLastBlockItr=False) %}
  {%- set item_padding = (item_config.get("padding", "")) | trim -%}
  {%- if item_padding not in ["shallow"] -%}
    {%- set item_padding = "" -%}
  {%- endif -%}
  {%- set item_classes = "" -%}
  {%- if item_padding | length > 0 -%}
    {%- set item_classes = "p-section--" + item_padding -%}
  {%- endif -%}
  {#- Last item of the last block should not have any bottom padding, as per desgin requirement -#}
  {%- if isLastLoopItr and isLastBlockItr -%}
    {%- set item_classes = "" -%}
  {%- endif -%}
  {{ item_classes | trim }}
{% endmacro %}
{% macro vf_divided_section(title, blocks=[], padding="default", is_split_on_medium=False, top_rule_variant="default", caller=None) %}
  {%- set padding = padding | trim -%}
  {%- if padding not in ["deep", "shallow", "default"] -%}
    {%- set padding = "default" -%}
  {%- endif -%}
  {%- set padding_classes = "p-section--" + padding -%}
  {%- if padding == "default" -%}
    {%- set padding_classes = "p-section" -%}
  {%- endif -%}
  <section class="{{ padding_classes }}">
    <div class="grid-row--50-50{%- if not is_split_on_medium -%}-on-large{%- endif -%}">
      {%- set description_block = blocks | selectattr("type", "equalto", "description-block") | first -%}
      {%- set divided_blocks = blocks | selectattr("type", "equalto", "divided-block") | list -%}
      {{ vf_section_top_rule(top_rule_variant) }}
      <div class="grid-col">{{- basic_section_title(title) -}}</div>
      <div class="grid-col">
        {%- if description_block -%}
          {%- for item in description_block.get("items",[]) -%}
            {% set item_classes = _get_item_padding_classes(item, loop.last) %}
            <div {%- if item_classes | trim | length > 0 %}
                 class="{{ item_classes }}"
                 {%- endif -%}>{{- basic_section_item(item) -}}</div>
          {%- endfor -%}
          {%- if divided_blocks | length > 0 -%}<hr class="p-rule--muted" />{%- endif %}
        {%- endif -%}
        {%- for block in divided_blocks -%}
          {%- set outer_last = loop.last -%}
          {%- set ns = namespace(list_element="ul", list_class="p-list") -%}
          {%- if block.get("bullet_type") == "number" -%}
            {%- set ns.list_element = "ol" -%}
            {%- set ns.list_class = "p-stepped-list" -%}
          {%- endif -%}
          <{{ ns.list_element }} class="p-divided__block {{ ns.list_class }} u-no-padding">
          {%- for item in block.get("items",[]) -%}
            {%- set list_bullet_type = "has-bullet" if block.bullet_type == "bullet" else "is-ticked" if block.bullet_type == "status" -%}
            <li class="p-divided__heading u-no-padding--bottom {{ list_bullet_type }}{% if ns.list_class == 'p-stepped-list' %} p-stepped-list__item{% else %} p-list__item{% endif %}">
              <div class="{% if not loop.last %}p-section--shallow{% endif %}{% if ns.list_class == 'p-stepped-list' %} p-stepped-list__title{% endif %}">
                {%- if item.title_text -%}
                  <h3 class="p-heading--5 u-no-padding">{{ item.title_text }}</h3>
                {%- endif -%}
                {%- for content in item.contents -%}
                  {%- set item_classes = _get_item_padding_classes(content, loop.last, outer_last) -%}
                  <div {%- if item_classes | trim | length > 0 %}
                       class="{{ item_classes }}"
                       {%- endif -%}>{{- basic_section_item(content) -}}</div>
                {%- endfor -%}
              </div>
            </li>
            {% if not loop.last %}<hr class="p-rule--muted" />{% endif %}
          {%- endfor -%}
          </ {{ ns.list_element }}>
        {%- endfor -%}
      </div>
    </div>
  </section>
{% endmacro %}
